package com.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.InitializingBean;
import com.entity.Insurance;
import com.service.InsuranceService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import com.entity.Result;
import com.utils.ResultUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.entity.User;

import java.util.Date;

import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping(value = "/admin/insurance")
public class AdminInsuranceController extends BaseController {

    @Autowired
    private InsuranceService insuranceService;


    @RequestMapping("add")
    public String add(HttpServletRequest request, Insurance model) {

        request.setAttribute("model", model);
        return "/admin/insurance-add";
    }


    @RequestMapping("edit")
    public String edit(HttpServletRequest request, String id) {
        Insurance model = insuranceService.getModel(id);
        request.setAttribute("model", model);
        return "/admin/insurance-add";
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(HttpServletRequest request, Insurance model) {
        User user = (User) request.getSession().getAttribute("user");
        int i = 0;
        if (StringUtils.isBlank(model.getId())) {
            model.preInsert();
            model.setCreateDate(new Date());
            model.setUserId(user.getId());
            i = insuranceService.insertModel(model);
        } else {
            i = insuranceService.updateModel(model);
        }
        if (i == 0) {
            return ResultUtil.error("失败");
        } else {
            return ResultUtil.success(null);
        }

    }


    //删除批量删除
    @RequestMapping("delete")
    @ResponseBody
    public Result delete(HttpServletRequest request, String id) {

        if (StringUtils.isNotBlank(id)) {
            String[] split = id.split(",");
            for (int i = 0; i < split.length; i++) {
                insuranceService.deleteModel(split[i]);
            }
            return ResultUtil.success();
        } else {
            return ResultUtil.error("未选中删除项！");
        }

    }


    @RequestMapping("list")
    public String list(HttpServletRequest request, Insurance model) {

        request.setAttribute("model", model);
        return "/admin/insurance-list";
    }

    //获取列表数据
    @RequestMapping("list/data")
    @ResponseBody
    public Result list(HttpServletRequest request, Insurance model, @RequestParam(defaultValue = "1") Integer page, Integer limit) {
        User user = (User) request.getSession().getAttribute("user");
        if (user.getType() == 0){
            model.setUserId(user.getId());
        }
        PageHelper.startPage(page, limit);
        List<Insurance> list = insuranceService.getModelList(putEntityInMap(model));
        PageInfo<Insurance> pageInfo = new PageInfo<>(list);
        return ResultUtil.success(list, pageInfo.getTotal());
    }


}